package com.pro_test.proservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pro_test.commonutils.R;
import com.pro_test.proservice.entity.ProHouse;
import com.pro_test.proservice.entity.ProOwner;
import com.pro_test.proservice.entity.ProPark;
import com.pro_test.proservice.entity.vo.ParkQuery;
import com.pro_test.proservice.service.ProParkService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Captain
 * @since 2022-02-26
 */
@RestController
@RequestMapping("/proservice/pro-park")
@CrossOrigin
public class ProParkController {
    @Autowired
    private ProParkService proParkService;

    @GetMapping("findAll")
    public R findAllPark()
    {
        List<ProPark> list = proParkService.list(null);
        return R.ok().data("items",list);
    }

    @DeleteMapping("{id}")
    public R removePark(@ApiParam(name = "id", value = "停车ID", required = true)
                         @PathVariable String id)
    {
        boolean flag = proParkService.removeById(id);
        if (flag)
        {
            return R.ok();
        }
        else
        {
            return R.error();
        }
    }

    //条件查询分页
    @ApiOperation(value = "条件分页查询")
    @PostMapping("pageParkCondition/{current}/{limit}")
    public R pageParkCondition(@PathVariable long current,
                                @PathVariable long limit,
                                @RequestBody(required = false) ParkQuery parkQuery)
    {
        //创建page对象
        Page<ProPark> pagePark = new Page<>(current,limit);

        //构建条件
        QueryWrapper<ProPark> wrapper = new QueryWrapper<>();
        String num = parkQuery.getNum();
        Integer status = parkQuery.getStatus();
        String begin = parkQuery.getBegin();
        String end = parkQuery.getEnd();

        if (!StringUtils.isEmpty(num))
        {
            wrapper.like("num",num);
        }
        if (!StringUtils.isEmpty(status))
        {
            wrapper.eq("status",status);
        }
        if (!StringUtils.isEmpty(begin))
        {
            wrapper.ge("gmt_create",begin);
        }
        if (!StringUtils.isEmpty(end))
        {
            wrapper.le("gmt_create",end);
        }
        //排序
        wrapper.orderByDesc("gmt_create");

        //调用方法实现条件查询分页
        proParkService.page(pagePark,wrapper);
        long total = pagePark.getTotal();
        List<ProPark> records = pagePark.getRecords();

        return R.ok().data("total",total).data("items",records);
    }

    @PostMapping("addPark")
    public R addPark(@RequestBody ProPark proPark)
    {
        boolean save = proParkService.save(proPark);
        if (save)
        {
            return R.ok();
        }
        else
        {
            return R.error();
        }
    }

    @GetMapping("getPark/{id}")
    public R getPark(@PathVariable String id)
    {
        ProPark proPark = proParkService.getById(id);
        List<ProPark> records = new ArrayList<>();
        records.add(proPark);
        return R.ok().data("park",proPark).data("proPark",records);
    }

    @GetMapping("getParkByOwnerId/{ownerId}")
    public R getParkByOwnerId(@PathVariable String ownerId)
    {
        Page<ProPark> pagePark = new Page<>();

        QueryWrapper<ProPark> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(ownerId))
        {
            wrapper.eq("owner_id",ownerId);
        }
        proParkService.page(pagePark,wrapper);
        long total = pagePark.getTotal();
        List<ProPark> records = pagePark.getRecords();

        return R.ok().data("total",total).data("proPark",records);
    }

    @PostMapping("updatePark")
    public R updatePark(@RequestBody ProPark proPark)
    {
        boolean flag = proParkService.updateById(proPark);
        if (flag)
        {
            return R.ok();
        }
        else
        {
            return R.error();
        }
    }
}

